package com.mbigger.mail.service;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service
public class MailService {

	@Value("${spring.mail.username}")
	private String from;

	@Autowired
	private JavaMailSender sender;

	/**
	 * 组装简单文本邮件
	 * 
	 * @param smm
	 * @param to
	 * @param subject
	 * @param text
	 * @throws MessagingException
	 */
	private void assemble(SimpleMailMessage smm, String to, String subject, String text) throws MessagingException {
		smm.setTo(to);
		smm.setSubject(subject);
		smm.setText(text);
		smm.setFrom(from);
	}

	/**
	 * 组装多媒体邮件
	 * 
	 * @param helper
	 * @param to
	 * @param subject
	 * @param text
	 * @param html
	 * @throws MessagingException
	 */
	private void assemble(MimeMessageHelper helper, String to, String subject, String text, boolean html)
			throws MessagingException {
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(text, html);
		helper.setFrom(from);
	}

	/**
	 * 组装带附件的多媒体邮件
	 * 
	 * @param helper
	 * @param to
	 * @param subject
	 * @param text
	 * @param html
	 * @param filePathList
	 * @throws MessagingException
	 */
	private void assemble(MimeMessageHelper helper, String to, String subject, String text, boolean html,
			String[] filePathList) throws MessagingException {
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(text, html);
		helper.setFrom(from);
		for (String filePath : filePathList) {
			FileSystemResource file = new FileSystemResource(new File(filePath));
			String fileName = file.getFilename();
			helper.addAttachment(fileName, file);
		}
	}

	/**
	 * 组装带图片的邮件
	 * 
	 * @param helper
	 * @param to
	 * @param subject
	 * @param text
	 * @param html
	 * @param resourcePath
	 * @param resourceId
	 * @throws MessagingException
	 */
	private void assemble(MimeMessageHelper helper, String to, String subject, String text, boolean html,
			String resourcePath, String resourceId) throws MessagingException {
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(text, html);
		helper.setFrom(from);
		FileSystemResource file = new FileSystemResource(new File(resourcePath));
		helper.addInline(resourceId, file);
	}

	public void sendSimpleMail(String to, String subject, String text) throws MessagingException {
		SimpleMailMessage ssm = new SimpleMailMessage();
		assemble(ssm, to, subject, text);
		sender.send(ssm);
	}

	public void sendHtmlMail(String to, String subject, String text) throws MessagingException {
		MimeMessage msg = sender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true);
		assemble(helper, to, subject, text, true);
		sender.send(msg);
	}

	public void sendAttachmentMail(String to, String subject, String text, String filePath) throws MessagingException {
		MimeMessage msg = sender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true);
		assemble(helper, to, subject, text, true, new String[] { filePath });
		sender.send(msg);
	}

	public void sendAttachmentsMail(String to, String subject, String text, String[] filePathList)
			throws MessagingException {
		MimeMessage msg = sender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true);
		assemble(helper, to, subject, text, true, filePathList);
		sender.send(msg);
	}

	public void sendInlineResourcesMail(String to, String subject, String text, String resourcePath, String resourceId)
			throws MessagingException {
		MimeMessage msg = sender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true);
		assemble(helper, to, subject, text, true, resourcePath, resourceId);
		sender.send(msg);
	}
}
